package com.one.group.admin.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.one.group.admin.mapper.InventoryFlowDetailMapper;
import com.one.group.admin.mapper.InventoryFlowMapper;
import com.one.group.model.dto.InventoryFlowAddDto;
import com.one.group.model.dto.InventoryFlowQueryDto;
import com.one.group.model.dto.InventoryFlowUpdateDto;
import com.one.group.model.dto.InventorySummaryUpdateDto;
import com.one.group.model.entity.MaterialPrice;
import com.one.group.model.entity.InventoryFlow;
import com.one.group.model.entity.InventoryFlowDetail;
import com.one.group.model.vo.InventoryFlowInformationVo;
import com.one.group.model.vo.InventoryFlowListVo;
import com.one.group.service.InventoryFlowDetailService;
import com.one.group.service.InventoryFlowService;
import com.one.group.service.InventorySummaryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class InventoryFlowServiceImpl extends ServiceImpl<InventoryFlowMapper, InventoryFlow> implements InventoryFlowService {
    @Autowired
    InventoryFlowDetailMapper inventoryFlowDetailMapper;
    @Autowired
    InventorySummaryService inventorySummaryService;
    @Override
    public List<InventoryFlowListVo> findInventoryFlowByQueryDto(InventoryFlowQueryDto inventoryFlowQueryDto) {
        return this.getMapper().findInventoryFlowByQueryDto(inventoryFlowQueryDto);
    }

    @Override
    @Transactional
    public boolean insertInventoryFlow(InventoryFlowAddDto inventoryFlowAddDto) {
//        先插入总表
        InventoryFlow inventoryFlow = new InventoryFlow();
        inventoryFlow.setNumber(inventoryFlowAddDto.getNumber());
        inventoryFlow.setStatus(inventoryFlowAddDto.getStatus());
        inventoryFlow.setPerson(inventoryFlowAddDto.getPerson());
        inventoryFlow.setRepositoryId(inventoryFlowAddDto.getRepositoryId());
        inventoryFlow.setVendorId(inventoryFlowAddDto.getVendorId());
        inventoryFlow.setRemark(inventoryFlowAddDto.getRemark());
        this.getMapper().insertSelective(inventoryFlow);
        List<MaterialPrice> materialPriceList = inventoryFlowAddDto.getMaterialPriceList();
//在插入详细流水表
        for (MaterialPrice materialPrice : materialPriceList) {
            InventoryFlowDetail inventoryFlowDetail = new InventoryFlowDetail();
            inventoryFlowDetail.setInventoryFlowId(inventoryFlow.getId());
            inventoryFlowDetail.setMaterialId(materialPrice.getMaterialId());
            inventoryFlowDetail.setPrice(materialPrice.getPrice());
            inventoryFlowDetail.setQuantity(materialPrice.getQuantity());
            inventoryFlowDetailMapper.insertSelective(inventoryFlowDetail);
        }
//        如果是采购入库那么添加，否则减少
        if (inventoryFlowAddDto.getStatus()==1){
            // 将materialPriceList里面每一个materialPrice的quantity变成一个相反数
            for (MaterialPrice materialPrice : materialPriceList) {
                if (materialPrice.getQuantity() != null) {
                    materialPrice.setPrice(materialPrice.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));
                    materialPrice.setQuantity(-materialPrice.getQuantity());
                }
            }
        }
        InventorySummaryUpdateDto inventorySummaryUpdateDto = new InventorySummaryUpdateDto();
        inventorySummaryUpdateDto.setMaterialPriceList(materialPriceList);
        inventorySummaryUpdateDto.setRepositoryId(inventoryFlow.getRepositoryId());
        inventorySummaryService.updateInventorySummaryByUpdateDto(inventorySummaryUpdateDto);
        return true;
    }

    @Override
    @Transactional
    public boolean updateInventoryFlow(InventoryFlowUpdateDto inventoryFlowUpdateDto) {
        InventoryFlow inventoryFlow = new InventoryFlow();
        BeanUtils.copyProperties(inventoryFlowUpdateDto,inventoryFlow);
        this.getMapper().insertOrUpdateSelective(inventoryFlow);
        Long id= inventoryFlowUpdateDto.getId();
        QueryWrapper queryWrapper = QueryWrapper.create().from(InventoryFlowDetail.class);
        queryWrapper.eq(InventoryFlowDetail::getInventoryFlowId,id);
        List<InventoryFlowDetail> inventoryFlowDetails = inventoryFlowDetailMapper.selectListByQuery(queryWrapper);

        inventoryFlowDetailMapper.deleteByQuery(queryWrapper);
//        新
        List<MaterialPrice> materialPriceList = inventoryFlowUpdateDto.getMaterialPriceList();
        for (MaterialPrice materialPrice : materialPriceList) {
            InventoryFlowDetail inventoryFlowDetail = new InventoryFlowDetail();
            inventoryFlowDetail.setInventoryFlowId(id);
            inventoryFlowDetail.setMaterialId(materialPrice.getMaterialId());
            inventoryFlowDetail.setPrice(materialPrice.getPrice());
            inventoryFlowDetail.setQuantity(materialPrice.getQuantity());
            inventoryFlowDetailMapper.insertSelective(inventoryFlowDetail);
        }
//        旧
        ArrayList<MaterialPrice> materialPrices = new ArrayList<>();
        for (InventoryFlowDetail inventoryFlowDetail:inventoryFlowDetails){
            MaterialPrice materialPrice = new MaterialPrice();
            materialPrice.setMaterialId(inventoryFlowDetail.getMaterialId());
            materialPrice.setQuantity(inventoryFlowDetail.getQuantity());
            materialPrice.setPrice(inventoryFlowDetail.getPrice());
            materialPrices.add(materialPrice);
        }
//        先将之前的反着来一遍，在操作新的。
        //        如果是采购入库那么添加，否则减少
        if (inventoryFlowUpdateDto.getStatus()==0){
            // 将materialPriceList里面每一个materialPrice的quantity变成一个相反数
            for (MaterialPrice materialPrice : materialPrices) {
                if (materialPrice.getQuantity() != null) {
                    materialPrice.setQuantity(-materialPrice.getQuantity());
                }
            }
        }
        if (inventoryFlowUpdateDto.getStatus()==1){
            for (MaterialPrice materialPrice : materialPrices) {
                if (materialPrice.getQuantity() != null) {
                    materialPrice.setPrice(materialPrice.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));

                }
            }
        }
        InventorySummaryUpdateDto inventorySummaryUpdateDto = new InventorySummaryUpdateDto();
        inventorySummaryUpdateDto.setMaterialPriceList(materialPrices);
        inventorySummaryUpdateDto.setRepositoryId(inventoryFlow.getRepositoryId());
        inventorySummaryService.updateInventorySummaryByUpdateDto(inventorySummaryUpdateDto);

        if (inventoryFlowUpdateDto.getStatus()==1){
            // 将materialPriceList里面每一个materialPrice的quantity变成一个相反数
            for (MaterialPrice materialPrice : materialPriceList) {
                if (materialPrice.getQuantity() != null) {
                    materialPrice.setPrice(materialPrice.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));
                    materialPrice.setQuantity(-materialPrice.getQuantity());
                }
            }
        }
        InventorySummaryUpdateDto inventorySummaryUpdateDto2 = new InventorySummaryUpdateDto();
        inventorySummaryUpdateDto2.setMaterialPriceList(materialPriceList);
        inventorySummaryUpdateDto2.setRepositoryId(inventoryFlow.getRepositoryId());
        inventorySummaryService.updateInventorySummaryByUpdateDto(inventorySummaryUpdateDto2);

        return true;
    }

    @Override
    public InventoryFlowInformationVo findInventoryFlowById(Long id) {
//        先查总表，再查详细表
        InventoryFlowInformationVo inventoryFlowInformationVo = new InventoryFlowInformationVo();
        InventoryFlow inventoryFlow = this.getMapper().selectOneById(id);
        BeanUtils.copyProperties(inventoryFlow,inventoryFlowInformationVo);
        QueryWrapper queryWrapper = QueryWrapper.create().from(InventoryFlowDetail.class);
        queryWrapper.eq(InventoryFlowDetail::getInventoryFlowId,id);
        List<InventoryFlowDetail> inventoryFlowDetails = inventoryFlowDetailMapper.selectListByQuery(queryWrapper);
        List<MaterialPrice> materialPriceList = new ArrayList<>();
        for (InventoryFlowDetail inventoryFlowDetail:inventoryFlowDetails){
            MaterialPrice materialPrice = new MaterialPrice();
            materialPrice.setMaterialId(inventoryFlowDetail.getMaterialId());
            materialPrice.setPrice(inventoryFlowDetail.getPrice());
            materialPrice.setQuantity(inventoryFlowDetail.getQuantity());
            materialPriceList.add(materialPrice);
        }
        inventoryFlowInformationVo.setMaterialPriceList(materialPriceList);
        return inventoryFlowInformationVo;
    }

    @Override
    @Transactional
    public boolean removeInventoryFlowById(Long id) {
        InventoryFlow inventoryFlow = this.getMapper().selectOneById(id);
        this.getMapper().deleteById(id);
        QueryWrapper queryWrapper = QueryWrapper.create().from(InventoryFlowDetail.class);
        queryWrapper.eq(InventoryFlowDetail::getInventoryFlowId,id);
        List<InventoryFlowDetail> inventoryFlowDetails = inventoryFlowDetailMapper.selectListByQuery(queryWrapper);
        ArrayList<MaterialPrice> materialPrices = new ArrayList<>();
        for (InventoryFlowDetail inventoryFlowDetail:inventoryFlowDetails){
            MaterialPrice materialPrice = new MaterialPrice();
            materialPrice.setMaterialId(inventoryFlowDetail.getMaterialId());
            materialPrice.setQuantity(inventoryFlowDetail.getQuantity());
            materialPrice.setPrice(inventoryFlowDetail.getPrice());
            materialPrices.add(materialPrice);
        }
//        先将之前的反着来一遍，在操作新的。
        //        如果是采购入库那么添加，否则减少
        if (inventoryFlow.getStatus()==0){
            // 将materialPriceList里面每一个materialPrice的quantity变成一个相反数
            for (MaterialPrice materialPrice : materialPrices) {
                if (materialPrice.getQuantity() != null) {
                    materialPrice.setQuantity(-materialPrice.getQuantity());
                }
            }
        }
        if (inventoryFlow.getStatus()==1){
            // 将materialPriceList里面每一个materialPrice的quantity变成一个相反数
            for (MaterialPrice materialPrice : materialPrices) {
                if (materialPrice.getQuantity() != null) {
                    materialPrice.setPrice(materialPrice.getPrice().multiply(BigDecimal.valueOf(2)).divide(BigDecimal.valueOf(3)));

                }
            }
        }


        InventorySummaryUpdateDto inventorySummaryUpdateDto = new InventorySummaryUpdateDto();
        inventorySummaryUpdateDto.setMaterialPriceList(materialPrices);
        inventorySummaryUpdateDto.setRepositoryId(inventoryFlow.getRepositoryId());
        inventorySummaryService.updateInventorySummaryByUpdateDto(inventorySummaryUpdateDto);
        inventoryFlowDetailMapper.deleteByQuery(queryWrapper);
        return true;
    }
}
